home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __all__ = [
- 'deque',
- 'defaultdict',
- 'namedtuple']
- from _abcoll import *
- import _abcoll
- __all__ += _abcoll.__all__
- from _collections import deque, defaultdict
- from operator import itemgetter as _itemgetter
- from keyword import iskeyword as _iskeyword
- import sys as _sys
-
- def namedtuple(typename, field_names, verbose = False):
- """Returns a new subclass of tuple with named fields.
-
- >>> Point = namedtuple('Point', 'x y')
- >>> Point.__doc__ # docstring for the new class
- 'Point(x, y)'
- >>> p = Point(11, y=22) # instantiate with positional args or keywords
- >>> p[0] + p[1] # indexable like a plain tuple
- 33
- >>> x, y = p # unpack like a regular tuple
- >>> x, y
- (11, 22)
- >>> p.x + p.y # fields also accessable by name
- 33
- >>> d = p._asdict() # convert to a dictionary
- >>> d['x']
- 11
- >>> Point(**d) # convert from a dictionary
- Point(x=11, y=22)
- >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
- Point(x=100, y=22)
-
- """
- if isinstance(field_names, basestring):
- field_names = field_names.replace(',', ' ').split()
-
- field_names = tuple(map(str, field_names))
- for name in (typename,) + field_names:
- if not all((lambda .0: for c in .0:
- if not c.isalnum():
- passc == '_')(name)):
- raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
- all((lambda .0: for c in .0:
- if not c.isalnum():
- passc == '_')(name))
- if _iskeyword(name):
- raise ValueError('Type names and field names cannot be a keyword: %r' % name)
- _iskeyword(name)
- if name[0].isdigit():
- raise ValueError('Type names and field names cannot start with a number: %r' % name)
- name[0].isdigit()
-
- seen_names = set()
- for name in field_names:
- if name.startswith('_'):
- raise ValueError('Field names cannot start with an underscore: %r' % name)
- name.startswith('_')
- if name in seen_names:
- raise ValueError('Encountered duplicate field name: %r' % name)
- name in seen_names
- seen_names.add(name)
-
- numfields = len(field_names)
- argtxt = repr(field_names).replace("'", '')[1:-1]
- reprtxt = ', '.join((lambda .0: for name in .0:
- '%s=%%r' % name)(field_names))
- dicttxt = ', '.join((lambda .0: for pos, name in .0:
- '%r: t[%d]' % (name, pos))(enumerate(field_names)))
- template = "class %(typename)s(tuple):\n '%(typename)s(%(argtxt)s)' \n\n __slots__ = () \n\n _fields = %(field_names)r \n\n def __new__(_cls, %(argtxt)s):\n return _tuple.__new__(_cls, (%(argtxt)s)) \n\n @classmethod\n def _make(cls, iterable, new=tuple.__new__, len=len):\n 'Make a new %(typename)s object from a sequence or iterable'\n result = new(cls, iterable)\n if len(result) != %(numfields)d:\n raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))\n return result \n\n def __repr__(self):\n return '%(typename)s(%(reprtxt)s)' %% self \n\n def _asdict(t):\n 'Return a new dict which maps field names to their values'\n return {%(dicttxt)s} \n\n def _replace(_self, **kwds):\n 'Return a new %(typename)s object replacing specified fields with new values'\n result = _self._make(map(kwds.pop, %(field_names)r, _self))\n if kwds:\n raise ValueError('Got unexpected field names: %%r' %% kwds.keys())\n return result \n\n def __getnewargs__(self):\n return tuple(self) \n\n" % locals()
- for i, name in enumerate(field_names):
- template += ' %s = _property(_itemgetter(%d))\n' % (name, i)
-
- if verbose:
- print template
-
- namespace = dict(_itemgetter = _itemgetter, __name__ = 'namedtuple_%s' % typename, _property = property, _tuple = tuple)
-
- try:
- exec template in namespace
- except SyntaxError:
- e = None
- raise SyntaxError(e.message + ':\n' + template)
-
- result = namespace[typename]
- if hasattr(_sys, '_getframe'):
- result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
-
- return result
-
- if __name__ == '__main__':
- from cPickle import loads, dumps
- Point = namedtuple('Point', 'x, y', True)
- p = Point(x = 10, y = 20)
- if not p == loads(dumps(p)):
- raise AssertionError
-
- class Point(namedtuple('Point', 'x y')):
- __slots__ = ()
-
- def hypot(self):
- return (self.x ** 2 + self.y ** 2) ** 0.5
-
- hypot = property(hypot)
-
- def __str__(self):
- return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
-
-
- for p in (Point(3, 4), Point(14, 5 / 7)):
- print p
-
-
- class Point(namedtuple('Point', 'x y')):
- '''Point class with optimized _make() and _replace() without error-checking'''
- __slots__ = ()
- _make = classmethod(tuple.__new__)
-
- def _replace(self, _map = map, **kwds):
- return self._make(_map(kwds.get, ('x', 'y'), self))
-
-
- print Point(11, 22)._replace(x = 100)
- Point3D = namedtuple('Point3D', Point._fields + ('z',))
- print Point3D.__doc__
- import doctest
- TestResults = namedtuple('TestResults', 'failed attempted')
- print TestResults(*doctest.testmod())
-
-